package net.sureon.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.common.SureonConstants;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class Index implements Controller {

	private String viewPage;
	private String loginAPI;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		AppCookie appCookie = new AppCookie(request.getCookies());
        Object siteUrlAttr = request.getAttribute(SureonConstants.REQ_ATTR_SITEURL);
        if(siteUrlAttr != null){
        	appCookie.setSiteURL((String) siteUrlAttr);
        }
		if (StringUtils.isNotEmpty(appCookie.getToken())) {
			request.getRequestDispatcher(getLoginAPI())
					.forward(request, response);
		}
		String sendMail = request.getParameter("sendMail");
		String registerCompany = request.getParameter("registerCompany");
		if("true".equals(sendMail)) {
			// 表示密码已通过mail发送给客户
			request.setAttribute("sendMail", "true");
		} 
		if("success".equals(registerCompany)) {
			// 表示公司注册成功
			request.setAttribute("registerCompany", "success");
		}
		ModelAndView view = new ModelAndView(viewPage);
		view.addObject("appCookie", appCookie);
		return view;
	}

	public String getViewPage() {
		return viewPage;
	}

	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}

	/**
	 * @return the loginAPI
	 */
	public String getLoginAPI() {
		return loginAPI;
	}

	/**
	 * @param loginAPI
	 *            the loginAPI to set
	 */
	public void setLoginAPI(String loginAPI) {
		this.loginAPI = loginAPI;
	}

}